window.onload = function () {
    // 创建地图实例
    var map = new BMap.Map("container");
    //起始地图坐标
    var point = new BMap.Point(118.756376, 32.052573);
    map.centerAndZoom(point, 1);
    // map.addControl(new BMap.ScaleControl());
    // map.addControl(new BMap.OverviewMapControl());
    // map.addControl(new BMap.MapTypeControl());
    map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
    map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用



    // 创建标注
    var marker = new BMap.Marker(point);
    marker.enableDragging();



    var routePolicy = [BMAP_DRIVING_POLICY_LEAST_TIME,BMAP_DRIVING_POLICY_LEAST_DISTANCE,BMAP_DRIVING_POLICY_AVOID_HIGHWAYS];
    //驾车路线
    var driving = new BMap.DrivingRoute(map, {
        renderOptions: {
            map: map,
            autoViewport: true
        },
        policy: routePolicy[1],   	//三种驾车策略：最少时间0，最短距离1，避开高速2
    });


    $(function () {

        var start_location="30.4082606,120.076693";
        var end_location="30.308260,120.0766935"

        $.ajax({
            url:'http://api.map.baidu.com/geocoder/v2/',
            type:'GET', //GET
            async:true,    //或false,是否异步
            data:{
                ak:"Hy39puRjkD52O5l2T0ne7ZpSepVNY3fN",
                location:start_location,
                output:"json",
                pois:"0",

            },
            timeout:5000,    //超时时间
            dataType:'JSONP',    //返回的数据格式：json/xml/html/script/jsonp/text
            success:function(data,textStatus,jqXHR){

                if(data.status==0){ //地址获取成功
                    var start=data.result.formatted_address;
                    console.log(start);
                    $.ajax({
                        url:'http://api.map.baidu.com/geocoder/v2/',
                        type:'GET', //GET
                        async:true,    //或false,是否异步
                        data:{
                            ak:"Hy39puRjkD52O5l2T0ne7ZpSepVNY3fN",
                            location:end_location,
                            output:"json",
                            pois:"0",

                        },
                        timeout:5000,    //超时时间
                        dataType:'JSONP',    //返回的数据格式：json/xml/html/script/jsonp/text
                        success:function(data,textStatus,jqXHR){

                            if(data.status==0){ //地址获取成功
                                var  end=data.result.formatted_address;
                                console.log(end);
                                driving.search(start, end);
                            }else{//地址获取失败
                                alert("地址获取失败2");
                            }

                        },
                        error:function(xhr,textStatus){
                            console.log('错误');
                            console.log(xhr);
                            alert("获取失败,url请求失败");
                        }

                    })

                }else{//地址获取失败
                    alert("地址获取失败1");
                }

            },
            error:function(xhr,textStatus){
                console.log('错误');
                console.log(xhr);
                alert("获取失败,url请求失败");
            }

        })


    })

}
